package com.baosight.risk.web.riim;

import java.util.List;
import java.util.Map;

import javax.ws.rs.core.MediaType;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baosight.risk.api.riim.RiimRiskAssignmentService;
import com.baosight.risk.api.riim.RiimRiskEvaluationService;
import com.baosight.risk.api.riim.RiimRiskGoalDetailService;
import com.baosight.risk.api.riim.RiimRiskGoalService;
import com.baosight.risk.api.riim.RiimRiskImportForSubService;
import com.baosight.risk.api.riim.RiimRiskImportService;
import com.baosight.risk.api.rire.RireRiskReportService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.base.entity.MultipleUpdateStatus;
import com.baosight.risk.common.annotation.Log;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.enums.BusinessType;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.ExcelUtil;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.riim.entity.RiimRiskAssignment;
import com.baosight.risk.service.riim.entity.RiimRiskEvaluation;
import com.baosight.risk.service.riim.entity.RiimRiskGoal;
import com.baosight.risk.service.riim.entity.RiimRiskGoalDetail;
import com.baosight.risk.service.riim.entity.RiimRiskImport;
import com.baosight.risk.service.riim.vo.AddDutyUserVO;
import com.baosight.risk.service.riim.vo.RiimRiskGoalVO;
import com.baosight.risk.service.riim.vo.RiimRiskImportVO;
import com.baosight.risk.service.riri.entity.RiriRisk;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@RestController
@RequestMapping("/riim/import")
@Api(value = "重点风险", description = "重点风险", produces = MediaType.APPLICATION_JSON)
public class RiimRiskImportController extends BaseController {

    @Autowired
    private RiimRiskImportService importService;

    @Autowired
    private RiimRiskEvaluationService evaluationService;

    @Autowired
    private RiimRiskAssignmentService assignmentService;

    @Autowired
    private RiimRiskGoalService goalService;

    @Autowired
    private RiimRiskGoalDetailService riskGoalDetailService;

    @Autowired
    private RireRiskReportService rireRiskReportService;
    @Autowired
    private RiimRiskImportForSubService riimRiskImportForSubService;
    
    /**
     * 根据ID查询重点风险
     *
     * @param id
     * @return
     */
    @GetMapping("/getById")
    @ApiOperation("根据ID查询重点风险")
    public AjaxResult edit(String id) {
        return success(importService.get(id));
    }

    /**
     * 查询重点风险列表
     *
     * @param riskImport
     * @return
     */
    @RequestMapping("/selectList")
    @ApiOperation("查询重点风险列表")
    public AjaxResult selectList(RiimRiskImportVO riskImport) {
        log.info("查询重点风险列表参数：" + riskImport.toString());
        return getResultPage(importService.selectList(riskImport, getUser()));
    }

    /**
     * 根据以及单位查询实施方案
     *
     * @param riskImport
     * @return
     */
    @RequestMapping("/queryShiShi")
    @ApiOperation("根据以及单位查询实施方案")
    public AjaxResult queryShiShi(RiimRiskImportVO riskImport) {
        log.info("查询重点风险列表参数：" + riskImport.toString());
        return getResultPage(importService.queryShiShi(riskImport));
    }


    /**
     * 根据IDS查询重点风险
     *
     * @return
     */
    @PostMapping("/getByIds")
    @ApiOperation("根据IDS查询重点风险")
    public AjaxResult getByIds(@RequestBody List<String> ids) {
        return getResultPage(importService.getByIds(ids));
    }


    /**
     * 重点风险删除
     *
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("重点风险删除")
    public AjaxResult delete(@RequestBody MultipleUpdateStatus updateStatus) throws Exception {
        if (StringUtils.isEmpty(updateStatus.getIds())) {
            return error("参数有误！");
        }
        List<String> ids = updateStatus.getIds();
        return success(importService.deleteImport(ids));

    }

    /**
     * 新增重点风险
     *
     * @param riskImport
     * @return
     */
    @PostMapping("/updateRiskImport")
    @ApiOperation("新增重点风险")
    public AjaxResult updateRiskImport(@RequestBody RiimRiskImport riskImport) throws Exception {
        return toAjax(importService.updateRiskImport(riskImport));
    }

    /**
     * 新增重点风险
     *
     * @param ririRiskList
     * @return
     */
    @PostMapping("/saveImportByRisk")
    @ApiOperation("新增重点风险")
    public AjaxResult saveImportByRisk(@RequestBody List<RiriRisk> ririRiskList) throws Exception {
        if (CollectionUtils.isEmpty(ririRiskList)) {
            log.error("数据为空！请至少选择一条数据！");
            return error("数据为空！请至少选择一条数据！");
        }
        return toAjax(importService.saveImportByRisk(ririRiskList));
    }


    /**
     * 修改重点风险
     *
     * @param riskImportList
     * @return
     */
    @PostMapping("/updateList")
    @Log(title = "修改重点风险", businessType = BusinessType.UPDATE)
    @ApiOperation("修改重点风险")
    public AjaxResult updateList(@RequestBody List<RiimRiskImport> riskImportList) {
        if (CollectionUtils.isEmpty(riskImportList)) {
            log.error("数据为空！请至少选择一条数据！");
            return error("数据为空！请至少选择一条数据！");
        }
        log.info("修改重点风险参数：" + riskImportList.toString());
        return success(importService.updateImportList(riskImportList));
    }

    /**
     * 流转重点风险
     *
     * @return
     */
    @PostMapping("/saveList")
    @Log(title = "流转重点风险")
    @ApiOperation("流转重点风险")
    public AjaxResult saveList(@RequestBody List<String> riskNoList) throws Exception {
        importService.saveImportList(riskNoList);
        return success();
    }

    /**
     * 下发重点风险
     *
     * @return
     */
    @PostMapping("/saveSendList")
    @Log(title = "下发重点风险")
    @ApiOperation("下发重点风险")
    public AjaxResult saveSendList(@RequestBody List<RiimRiskImport> riskImportList){
    	try {
			importService.saveSendList(riskImportList);
			return success();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return error(e.getMessage());
		}
    }


    @PostMapping("updateStatus")
    @Log(title = "重点风险状态修改")
    @ApiOperation("重点风险状态修改")
    public AjaxResult updateStatus(@RequestBody MultipleUpdateStatus updateStatus) {
        List<String> ids = updateStatus.getIds();
        String status = updateStatus.getStatus();
        String statusName = updateStatus.getStatusName();
        if (CollectionUtils.isEmpty(ids)) {
            log.error("参数有误！");
            return error("参数有误！");
        }
        if (StringUtils.isEmpty(status)) {
            log.error("参数有误！");
            return error("参数有误！");
        }
        for (String id : ids) {
            if (null == importService.get(id)) {
                log.error("数据有误！");
                return error("数据有误！");
            }
            importService.updateStatus(id, status, statusName);
        }
        return AjaxResult.success();
    }


    /**
     * 重点风险实施方案填报
     *
     * @param importVO
     * @return
     */
    @PostMapping("/updateImport")
    @Log(title = "填报/修改重点风险==实施方案")
    @ApiOperation("填报/修改重点风险==实施方案")
    public AjaxResult updateImport(@RequestBody RiimRiskImportVO importVO) throws Exception {
        importVO = (RiimRiskImportVO) BeanUtils.updateBean(importVO);
        log.info("重点风险实施方案填报参数：" + importVO.toString());
        return success(importService.updateImport(importVO));
    }

    /**
     * 重点风险实施方案查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getImport")
    @ApiOperation("重点风险实施方案查询")
    public AjaxResult getImport(@RequestParam("id") String id) {
        return success(importService.getImport(id));
    }

    @GetMapping("/selectByType")
    @ApiOperation("实施方案项目目标明细")
    public AjaxResult selectByType(String goalId, String typeFlag) {
        if (StringUtils.isEmpty(goalId) || StringUtils.isEmpty(typeFlag)) {
            log.error("数据有误！");
            return error("数据有误！");
        }
        return success(importService.selectByType(goalId, typeFlag));
    }

    @GetMapping("/saveSendReport")
    @ApiOperation("重点风险下发披露报告")
    public AjaxResult saveSendReport() throws Exception {
        return success(importService.saveSendReport("","",""));
    }

    @PostMapping("/sendAdvance")
    @ApiOperation("重点风险分配推进人")
    public AjaxResult sendAdvance(@RequestBody MultipleUpdateStatus updateStatus) {
        List<String> ids = updateStatus.getIds();
        if (StringUtils.isEmpty(ids)) {
            return error("数据有误！");
        }
        importService.updateStatus(ids, updateStatus.getExt1(), updateStatus.getExt2(), updateStatus.getExt3(), updateStatus.getExt4());
        return success();
    }

    @PostMapping("/saveEvaluation")
    @ApiOperation("重点风险应对策略保存")
    public AjaxResult saveEvaluation(@RequestBody RiimRiskEvaluation evaluation) {
        if (evaluation == null) {
            return error("数据有误！");
        }
        return success(evaluationService.saveEvaluation(evaluation));
    }

    @PostMapping("/deleteEvaluation")
    @ApiOperation("重点风险应对策略删除")
    public AjaxResult deleteEvaluation(@RequestBody MultipleUpdateStatus updateStatus) {
        List<String> ids = updateStatus.getIds();
        if (StringUtils.isEmpty(ids)) {
            return error("数据有误！");
        }
        return success(evaluationService.deleteEvaluation(ids));
    }

    @PostMapping("/saveAssignment")
    @ApiOperation("重点风险主要职责及分工保存")
    public AjaxResult saveAssignment(@RequestBody RiimRiskAssignment assignment) {
        if (assignment == null) {
            return error("数据有误！");
        }
        return success(assignmentService.saveAssignment(assignment));
    }

    @PostMapping("/deleteAssignment")
    @ApiOperation("重点风险主要职责及分工删除")
    public AjaxResult deleteAssignment(@RequestBody MultipleUpdateStatus updateStatus) {
        List<String> ids = updateStatus.getIds();
        if (StringUtils.isEmpty(ids)) {
            return error("数据有误！");
        }
        return success(assignmentService.deleteAssignment(ids));
    }

    @PostMapping("/saveGoal")
    @ApiOperation("重点风险项目目标保存")
    public AjaxResult saveGoal(@RequestBody RiimRiskGoalVO goalVO) {
        if (goalVO == null) {
            return error("数据有误！");
        }
        return success(goalService.saveGoal(goalVO));
    }

    @PostMapping("/saveImport")
    @ApiOperation("重点风险单个保存")
    public AjaxResult saveImport(@RequestBody List<RiimRiskImport> riskImportList) {
        if (CollectionUtils.isEmpty(riskImportList)) {
            return error("数据有误！");
        }
        for (RiimRiskImport riskImport : riskImportList) {
            importService.saveImport(riskImport);
        }
        return success();
    }

    @PostMapping("/deleteGoal")
    @ApiOperation("重点风险项目目标删除")
    public AjaxResult deleteGoal(@RequestBody MultipleUpdateStatus updateStatus) {
        List<String> ids = updateStatus.getIds();
        if (StringUtils.isEmpty(ids)) {
            return error("数据有误！");
        }
        return success(goalService.deleteGoal(ids));
    }

    /**
     * 导出excel
     *
     * @return
     */
    @PostMapping("export")
    @ResponseBody
    @ApiOperation("导出重点风险")
    public AjaxResult exportExcel(@RequestBody RiimRiskImportVO importVO) {
        log.info("导出重点风险：" + importVO.toString());
        ExcelUtil<RiimRiskImport> excel = new ExcelUtil<>(RiimRiskImport.class);
        return excel.exportExcel(importService.selectList(importVO, null), "重点风险", importVO.getColumNameList());
    }

    /**
     * 更改排序
     *
     * @param riskImport
     * @return
     */
    @PostMapping("updateRowOrder")
    @ApiOperation("排序")
    public AjaxResult updateRowOrder(@RequestBody RiimRiskImportVO riskImport) {
        importService.updateRowOrder(riskImport, getUser());
        return success();

    }

    @PostMapping("/saveGoalDetail")
    @ApiOperation("项目目标子表保存")
    public AjaxResult saveGoalDetail(@RequestBody List<RiimRiskGoalDetail> goalDetailList) {
        if (CollectionUtils.isEmpty(goalDetailList)) {
            return error("数据有误！");
        }
        return success(riskGoalDetailService.saveRiskGoalDetail(goalDetailList));
    }

    @PostMapping("/deleteGoalDetail")
    @ApiOperation("重点风险主要职责及分工删除")
    public AjaxResult deleteGoalDetail(@RequestBody MultipleUpdateStatus updateStatus) {
        List<String> ids = updateStatus.getIds();
        if (StringUtils.isEmpty(ids)) {
            return error("数据有误！");
        }
        riskGoalDetailService.deleteRiskGoalDetail(ids);
        return success();
    }

    /**
     * 实施方案,风险评估排序上下移
     *
     * @param evaluation
     * @return
     */
    @PostMapping("evaluationRowOrder")
    @ApiOperation("排序")
    public AjaxResult updateRowOrderEvaluation(@RequestBody RiimRiskEvaluation evaluation) {
        evaluationService.updateRowOrder(evaluation);
        return success();
    }

    /**
     * 实施方案,项目目标排序上下移
     *
     * @param riskGoal
     * @return
     */
    @PostMapping("goalRowOrder")
    @ApiOperation("排序")
    public AjaxResult updateRowOrderEvaluation(@RequestBody RiimRiskGoal riskGoal) {
        goalService.updateRowOrder(riskGoal);
        return success();
    }

    /**
     * 实施方案,主要职责及分工排序上下移
     *
     * @param assignment
     * @return
     */
    @PostMapping("assignmentRowOrder")
    @ApiOperation("排序")
    public AjaxResult updateRowOrderEvaluation(@RequestBody RiimRiskAssignment assignment) {
        assignmentService.updateRowOrder(assignment);
        return success();
    }

    /**
     * 更改重点风险项目责任人
     */
    @GetMapping("doChangeRes")
    @ApiOperation("更改重点风险项目责任人")
    public AjaxResult doChangeRes( RiimRiskImport riimRiskImport) {
        Integer integer = importService.doChangeRes(riimRiskImport);
        return toAjax(integer);
    }

    /**
     * 实施方案待分配节点新增项目推进人
     *
     * @param
     * @return
     */
    @PostMapping("addDutyUser")
    @ApiOperation("排序")
    public AjaxResult addDutyUser(@RequestBody AddDutyUserVO addDutyUserVO) throws Exception {
        importService.addDutyUser(addDutyUserVO.getEmployee(), addDutyUserVO.getRiimRiskImport());
        return success();
    }

    /**
     * 复制重点风险 状态为草稿  待填报
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("copy")
    @ApiOperation("重点风险清单复制")
    public AjaxResult copyRiskImport(@RequestBody Map<String,Object> map) throws Exception {
        //@RequestParam("ids") String[] ids,@RequestParam("year") String year,@RequestParam("quarter") String quarter
//        if (CollectionUtils.isEmpty(riskImportList)) {
//            log.error("数据为空！请至少选择一条数据！");
//            return error("数据为空！请至少选择一条数据！");
//        }
        //return toAjax(importService.copyImportByRisk(map));
        List<String> ids = (List<String>) map.get("ids");
        String year = (String) map.get("year");
        String quarter = (String) map.get("quarter");
        importService.copyImportByRisk(ids,year,quarter);
        return success();
    }

    /**
     * 复制并下发
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping("copyAndIssue")
    @ApiOperation("重点风险清单复制并下发")
    public AjaxResult copyAndIssue(@RequestBody Map<String,Object> map) throws Exception {
        List<String> ids = (List<String>) map.get("ids");
        String year = (String) map.get("year");
        String quarter = (String) map.get("quarter");
        importService.copyAndIssue(ids,year,quarter);
        return success();
    }

    @PostMapping("saveConclusion")
    @ApiOperation("重点风险结题下发待办")
    public AjaxResult saveConclusion(@RequestBody List<RiimRiskImport> list) throws Exception {
        if (list.isEmpty()){
            return error("数据为空！");
        }
        importService.saveConclusion(list);
        return success();
    }

    /**
     * 批量删除披露报告
     * @param ids
     * @return
     */
    @PostMapping("/deleteRiskWarningStatus")
    @ApiOperation("删除风险披露报告")
    public AjaxResult deleteRiskWarningStatus(@RequestBody List<String> ids) throws Exception {
        if (ids.isEmpty()){
            return error("数据为空！");
        }
        rireRiskReportService.deleteRiskWarningStatus(ids);
        return success();
    }

    /**
     * 推送
     * @param ids
     * @return
     */
    @GetMapping("/pushRisk")
    @ApiOperation("推送")
    public AjaxResult pushRisk(String id){
        try {
			riimRiskImportForSubService.pushRisk(id);
			return success();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return error(e.getMessage());
		}
    }

}
